package isi_graf;

import java.util.Date;
import java.util.Iterator;

import isi_ib.barra_trade;
import isi_ib.barras_trade;
import isi_ib.isi_estudios;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;



public class isiTimechart {

	private XYDataset dataset;
	private JFreeChart chart;
	private ChartPanel chartPanel;
	String titulo;
	String tipo;
	
	
	public  isiTimechart(String titulo_,String tipo_ ) {
		super();
		dataset=null;
		chart=null;
		chartPanel=null;
		titulo=titulo_;
		tipo=tipo_;
	}
	
	public ChartPanel CreaChart(barras_trade barras_){
		if(tipo=="st")		dataset = createDataset(barras_);
		else 		dataset = createDataset1(barras_);
		chart = createChart(dataset);
		Ajusta(barras_.minvalue,barras_.maxvalue);
		chartPanel = new ChartPanel(chart);
		return chartPanel;
	}
	
	private void Ajusta(Double minvalue_,Double maxvalue_){
		if(chart==null) return;
	/*	if(minvalue_==0 && maxvalue_==0){
			minvalue_ = getLowestLow(dataset);
			maxvalue_ = getHighestHigh(dataset);
		}*/
		chart.getXYPlot().getRangeAxis().setRange(minvalue_,maxvalue_);
	}
		
	
	
	private JFreeChart createChart(XYDataset dataset_) {
			  if(dataset==null) return null; 
			  JFreeChart chart = ChartFactory.createTimeSeriesChart(
					  titulo,
			            "Date", 
			            "Value",
			            dataset,
			            true,
			            true,
			            false
			        );
			  return chart;
			  }
	
	
	
	private XYDataset createDataset1(barras_trade barras_) {

		 TimeSeriesCollection dataset = new TimeSeriesCollection();
	     dataset.setDomainIsPointsInTime(true);
       
	     
		//XYSeriesCollection dataset = new XYSeriesCollection();	 
	     final TimeSeries series1 = new TimeSeries("Series 1", Minute.class);
	     final TimeSeries series2 = new TimeSeries("Series 2", Minute.class);
		isi_estudios estudios = new isi_estudios(barras_,null,"",false);
		estudios.IniciaOscilador(14,3,2,0);
	    Iterator<barra_trade> itr = barras_.barras.iterator();
			  int i=0;
			    while (itr.hasNext()) {
			    	barra_trade element=itr.next();
			    	int aaa_=barras_.barras.indexOf(element);
			    	series1.add(new Minute(element.getDate()), element.getClose());
			    	series2.add(new Minute(element.getDate()), element.getOpen());
			    	
			    	//series1.add(i, estudios.Media_movil_exponencial(element,6,"close"));
			    	//series2.add(i, estudios.Media_simple(element,3,"close"));
			      i++;
			    }
			    dataset.addSeries(series1);		
			    dataset.addSeries(series2);	
			  return dataset;
			  }
	
	private XYDataset createDataset(barras_trade barras_) {

		 TimeSeriesCollection dataset = new TimeSeriesCollection();
	     dataset.setDomainIsPointsInTime(true);
        
	     	     
		//XYSeriesCollection dataset = new XYSeriesCollection();	 
	     final TimeSeries series1 = new TimeSeries("K lento", Minute.class);
	     final TimeSeries series2 = new TimeSeries("D Lento", Minute.class);
//	     final TimeSeries series3 = new TimeSeries("Klento media", Minute.class);
	     
	     final TimeSeries series20 = new TimeSeries("20", Minute.class);
	     final TimeSeries series80 = new TimeSeries("80", Minute.class);
	     
	     
	     
		isi_estudios estudios = new isi_estudios(barras_,null,"",false);
		
		
		estudios.IniciaOscilador(14,3,9,1);
			  Iterator<barra_trade> itr = barras_.barras.iterator();
			  int i=0;
			    while (itr.hasNext()) {
			    	barra_trade element=itr.next();
			    	int aaa_=barras_.barras.indexOf(element);
			    	//series1.add(new Minute(element.getDate()), estudios.OsciladorKLento(barras_.barras.indexOf(element)));
			    	//series2.add(new Minute(element.getDate()), estudios.OsciladorDLento(barras_.barras.indexOf(element)));
//			    	series3.add(new Minute(element.getDate()), estudios.OsciladorKLentoMedia(barras_.barras.indexOf(element)));
			    	series20.add(new Minute(element.getDate()), 20);
			    	series80.add(new Minute(element.getDate()), 80);
			    	
			    	//series1.add(i, estudios.Media_movil_exponencial(element,6,"close"));
			    	//series2.add(i, estudios.Media_simple(element,3,"close"));
			      i++;
			    }
			    dataset.addSeries(series1);		
			    dataset.addSeries(series2);	
//			    dataset.addSeries(series3);	
			    dataset.addSeries(series20);	
			    dataset.addSeries(series80);	
			  return dataset;
			  }
	
}
